#include <stdlib.h>
#include <stdio.h>
#include "base.h"
#include "fivekernel.h"
#include "gui.h"
#include "if_all.h"
#include "singleplay.h"

#define MSG_VERSION_STR		"0.09 alpha (svn-2006-12-07)"

#define BOARD_VIEW_X	0
#define BOARD_VIEW_Y	0
#define BOARD_VIEW_X2	800
#define BOARD_VIEW_Y2	600

Image img_board;
//Image img_bgmask;
Image img_black;
Image img_white;

//当棋盘点击时调用，x, y是点击坐标
void ACALLBACK (*board_click)(int x, int y);

int board_draw_x, board_draw_y;

void ACALLBACK render_board_area(struct Panel *pn, void *userdata)
{
	int i, j;
	
	int px, py;
	panel_get_abs_pos(pn, &px, &py);
	//对于渲染,总是得取得它的绝对坐标,这样才能在屏幕上直接绘图
	
	video_drawimg(img_board, board_draw_x + px, board_draw_y + py);
	for (i = 0; i < 15; ++i) {
		for (j = 0; j < 15; ++j) {
			if (global_board[i][j] == BLACK) {
				video_drawimg(img_black,
				j * 48 + board_draw_x + px,
				i * 48 + board_draw_y + py);
			}
			else if (global_board[i][j] == WHITE) {
				video_drawimg(img_white,
				      j * 48 + board_draw_x + px,
				      i * 48 + board_draw_y + py);
			}
		}
	}
	
	int x, y;
	krnl_get_last_pos(&x, &y);
	if (x != -1 && y != -1) {
		x = x * 48 + board_draw_x + px;
		y = y * 48 + board_draw_y + py;
		video_set_color(255, 0, 0);
		video_rectangle(x, y, x + 48, y + 48);
	}
}

//棋盘区域数据及回调
static int button_flag;
static int move_dx, move_dy;

void ACALLBACK board_area_down(struct Panel *pn,
			int button, int x, int y, void *userdata)
{
	if (button == 3) {
		button_flag = 1;
		move_dx = x - board_draw_x;
		move_dy = y - board_draw_y;
	} else if (button == 1) {
		int px, py;
		px = (x - board_draw_x) / 48;
		py = (y - board_draw_y) / 48;
		if (px >= 0 && px < 15 && py >= 0 && py < 15) {
			if (board_click) (*board_click)(px, py);
		}
	}
}

void ACALLBACK board_area_up(struct Panel *pn,
			     int button, int x, int y, void *userdata)
{
	if (button == 3) {
		button_flag = 0;
	}
}


void ACALLBACK board_area_move(struct Panel *pn, int x, int y, void *userdata)
{
	if (button_flag == 1) {
		board_draw_x = x - move_dx;
		if (board_draw_x > BOARD_VIEW_X + 100)
			board_draw_x = BOARD_VIEW_X + 100;

		if (board_draw_x + img_get_width(img_board) <
				  BOARD_VIEW_X2 - 250)
			board_draw_x = BOARD_VIEW_X2 - 250 -
					img_get_width(img_board);

		board_draw_y = y - move_dy;
		if (board_draw_y > BOARD_VIEW_Y + 100)
			board_draw_y = BOARD_VIEW_Y + 100;
		if (board_draw_y + img_get_height(img_board) <
				  BOARD_VIEW_Y2 - 250)
			board_draw_y = BOARD_VIEW_Y2 - 250 -
					img_get_height(img_board);
	}
}


int main(int argc, char **argv)
{
	if (all_init("")) return -1;
	video_set_caption("Cbar five");
	video_set_bkcolor(128, 128, 128);
	krnl_init();
	if (gui_init()) return -1;
	
	if ((img_board = video_loadimg("resources/board-ff.png")) == NULL) {
		fprintf(stderr, "failed to open board image.\n");
		exit(-1);
	}
	
	if ((img_black = video_loadimg("resources/black.png")) == NULL) {
		fprintf(stderr, "failed to open black image.\n");
		exit(-1);
	}
	if ((img_white = video_loadimg("resources/white.png")) == NULL) {
		fprintf(stderr, "failed to open white image.\n");
		exit(-1);
	}
	

	struct Panel *board_panel;
	board_panel = panel_new(NULL, 0, 0, 800, 600);
	panel_mouse_down(board_panel, board_area_down);
	panel_mouse_up(board_panel, board_area_up);
	panel_mouse_move(board_panel, board_area_move);
	panel_user_render(board_panel, render_board_area);
	board_draw_x = (800 - img_get_width(img_board)) / 2;
	board_draw_y = (600 - img_get_height(img_board)) / 2;

	
	struct Msgbox *msg_welcome = msgbox_new("Welcome", "cbar-five\n  Version:" MSG_VERSION_STR"\nvisit our homepage for more imformation:\nhttp://code.google.com/p/cbar-five/", 0, MSG_OK);
	msgbox_run(msg_welcome);
	msgbox_delete(msg_welcome);
	
/*---------------8<----------------*/
	input_new(NULL, 50, 200, 400, 200);
//	input_input(a, "234234");
//	input_set_cur(a, 0);
//	input_input(a, "abcde");
//	input_set_cur(a, 1);
//	input_input(a, "fffffff");
/*---------------8<----------------*/

	
	main_menu_init();
	play_panel_init();
	
	int res = 0;
	while (res != 4) {		//4为quit
		res = main_menu_run();
		switch (res) {
		case 0:		//选择1 player
			board_click = single_board_click;
			play_panel_show();
			play_panel_set_callback(single_new,
					single_retract,
					single_save,
					single_load,
					single_ret);
			single_game_start();
			break;
		case 1:
		case 2:
		case 3:
			{
				struct Msgbox *msg_error = msgbox_new("Cbar-five", "Not yet complete...", 0, MSG_OK);
				msgbox_run(msg_error);
				msgbox_delete(msg_error);
			}
			break;
		}
	}
	
	//msgbox_new("null", "You Win", 0, MSG_OK | MSG_YES | MSG_NO | MSG_CANCEL);
	SDL_Quit();
	return 0;
}

